package main

import (
	"context"
	"fmt"
	"demo1/message"
	"errors"
	"github.com/micro/go-micro/v2"
)
/**
API 网管
rpc:用于后端各个微服务之间的服务调用通信
rest api:用于前端通过http协议实现于后端的通信交互

Micro框架网管功能：API网管的作用就是为微服务做代理。负责将微服务的RPC方法代理成支持HTPP协议的web请求，同时将用户端使用的url进行暴露
网管功能需下载micro/micro：[本项目使用v2版本对应go-micro/v2]
1.下载源码
v1.0: go get github.com/micro/micro
v2.0: go get github.com/micro/micro/v2
v3.0: go get github.com/micro/micro/v3
2.安装
go install github.com/micro/micro
go install github.com/micro/micro/v2
go install github.com/micro/micro/v3
3.校验micro是否安装成功
micro --version
4.开启api getway监听
micro api
micro api --handler=http rpc反向代理成为http请求方式

5.go-restful库实现restfull风格的路径映射
go get github.com/emicklei/go-restful
 */
type StudentManager struct {}

func (h *StudentManager) GetStudent(ctx context.Context, in *message.StudentRequest, out *message.Student) error {
	fmt.Println("接收到client端请求...")
	studentMap := map[string]message.Student{
		"jimmy":message.Student{Name:"jimmy",Classes:"二年一班",Grade:65},
		"ztind":message.Student{Name:"ztind",Classes:"二年二班",Grade:88},
		"jack":message.Student{Name:"jack",Classes:"二年三班",Grade:100},
	}
	student := studentMap[in.Name]
	if student.Name!=""{
		*out = student
		return nil
	}
	return errors.New(in.Name + "not exists")
}

func main(){
	//创建一个服务
	service := micro.NewService(
		micro.Name("go.micro.srv.st"),
		micro.Version("v1.0.0"),
	)
	//初始化服务的option配置
	service.Init()
	//注册服务处理
	////注意：Cannot use 'service.Server()' (type "github.com/micro/go-micro/server".Serve 出现这种错误到gopath/pkg/mod里删除重复的包即可
	message.RegisterSetudenServiceHandler(service.Server(),&StudentManager{})
	//运行服务
	service.Run()
}
